﻿package bdz.projects.lavender.doi.ui
{
	import bdz.common.events.GenericEvent;
	import bdz.projects.lavender.doi.utils.DOIAudio;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.*;
	
	import gs.*;
	import gs.easing.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class DOIQuickMenuSub extends MovieClip 
	{
		private var _local:Array;
		private var _items:Array;
		private var _headerRenderer:String;
		private var _itemRenderer:String;
		private var _audio:DOIAudio;
		public function DOIQuickMenuSub(defInfo:Array, headerrenderer:String, itemrenderer:String) {
			_local = defInfo;
			_headerRenderer = headerrenderer;
			_itemRenderer = itemrenderer;
			
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		private function initializeComponent(evt:Event):void {
			_audio = DOIAudio.getInstance();
			var len:Number = _local.length;
			var sy:Number = 0;
			var sx:Number = 0;
			_items = new Array();
			for each(var obj:Object in _local) {
				var hclass:Object = getDefinitionByName(_headerRenderer);
				var iclass:Object = getDefinitionByName(_itemRenderer);
				var children:Array = obj.steps;
				var href:MovieClip = new hclass();
				href.deftext = obj.name;
				href.definfo = obj;
				//href.x = -150;
				href.alpha = 0;
				href.y = sy;
				//href.buttonMode = href.useHandCursor = true;
				href.mouseChildren = false;
				//href.addEventListener(MouseEvent.ROLL_OVER, rollActions);
				//href.addEventListener(MouseEvent.ROLL_OUT, rollActions);
				//href.addEventListener(MouseEvent.CLICK, clickActions);
				sy += href.height;
				_items.push(href);
				addChild(href);
				for each(var sobj:Object in children) {
					var iref:MovieClip = new iclass();
					iref.deftext = sobj.name.split("|").join(" ");
					iref.definfo = sobj;
					iref.y = Math.ceil(sy);
					//iref.x = -150;
					iref.alpha = 0;
					iref.buttonMode = iref.useHandCursor = true;
					iref.mouseChildren = false;
					iref.addEventListener(MouseEvent.ROLL_OVER, rollActions);
					iref.addEventListener(MouseEvent.ROLL_OUT, rollActions);
					iref.addEventListener(MouseEvent.CLICK, clickActions);
					sy += iref.height;
					_items.push(iref);
					addChild(iref);
				}
			}
			var _delay:Number = 0;
			for each(var mc:MovieClip in _items) {
				TweenMax.to(mc, 1, { delay:_delay, ease:Strong.easeOut, alpha:1 } );
				_delay += .03;
			}
		}
		private function clickActions(evt:MouseEvent):void {
			_audio.playTick();
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED, evt.target.definfo));
		}
		private function rollActions(evt:MouseEvent):void {
			
			if (evt.target is qsubmenuheader) {
				evt.target.tlabel.textColor = evt.type == MouseEvent.ROLL_OVER ? 0x8C7D70 : 0xffffff;
			}
			if (evt.type == MouseEvent.ROLL_OVER) {
				_audio.playTick();
			}
			TweenMax.to(evt.target.highlight, .6, { scaleY:evt.type == MouseEvent.ROLL_OVER ? 1 : 0, ease:Strong.easeOut } );
			
		}
		public function remove():void {
			
			var _delay:Number = 0;
			for each(var mc:MovieClip in _items){
				TweenMax.to(mc, 1, {ease:Strong.easeOut, delay:_delay, alpha:0, onComplete:removeChild, onCompleteParams:[mc] } );
				_delay += .03;
			}
		}
		
	}
	
}